package com.example.graduation.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduation.entity.Address;
import com.example.graduation.form.AddressForm;
import com.example.graduation.form.PageSizeForm;
import com.example.graduation.service.impl.AddressServiceImpl;
import com.example.graduation.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressServiceImpl addressService;


    /**
     * @api {POST} /address/updateAddress    添加或者修改收获地址
     * @apiVersion 1.0.0
     * @apiGroup AddressController
     * @apiName updateAddress
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=0DWc4
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {Number} userId
     * @apiParam (请求体) {String} userName
     * @apiParam (请求体) {String} phone
     * @apiParam (请求体) {String} province
     * @apiParam (请求体) {String} city
     * @apiParam (请求体) {String} county
     * @apiParam (请求体) {String} detailed
     * @apiParam (请求体) {String} number
     * @apiParam (请求体) {Number} title
     * @apiParam (请求体) {Number} status
     * @apiParamExample 请求体示例
     * {"number":"YpaqDmmow","province":"nE","phone":"LQr0UlDFp","city":"zZtqKs62","detailed":"ifd2HJts1","county":"fiUqbpHZ","id":3150,"userName":"YLLekB","title":8245,"userId":7669,"status":9074}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/updateAddress")
    @ResponseBody
    public R updateAddress(@RequestBody Address address, @RequestHeader String token) {
        addressService.updateAddress(address, token);
        return R.ok("添加成功").put("address", address);
    }


    /**
     * @api {POST} /address/findAddress 分页查询用户收货地址
     * @apiVersion 1.0.0
     * @apiGroup AddressController
     * @apiName findAddress
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=JA0yxu
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParamExample 请求体示例
     * {"size":1357,"page":3471}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findAddress")
    @ResponseBody
    public R findAddress(@RequestBody PageSizeForm form, @RequestHeader String token) {
        Page<Address> address = addressService.findAddress(token, form.getPage(), form.getSize());
        return R.ok("查找成功").put("address", address);
    }


    /**
     * @api {POST} /address/deleteAddress 根据地址id删除地址
     * @apiVersion 1.0.0
     * @apiGroup AddressController
     * @apiName deleteAddress
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=OoY
     * @apiParam (请求体) {Number} addressid
     * @apiParamExample 请求体示例
     * {"addressid":8390}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/deleteAddress")
    @ResponseBody
    public R deleteAddress(@RequestHeader String token, @RequestBody AddressForm form) {
        boolean flag = addressService.deleteAddress(token, form.getAddressid());
        if (flag == true) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }


}

